﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleTesrt.InversionOfControl;
using ConsoleTesrt.GameTestIOC;
using ConsoleTesrt.LINQTest;
using System.Text.RegularExpressions;
namespace ConsoleTesrt
{
    class Program
    {
        static int fun(int a, int acc)
        {
            while (true)
            {
                if (a == 0) return acc;
                acc *= a;
                a--;
                Console.WriteLine(acc);
            }
        }

        static void testaction(string ass, Action<string> a)
        {
            a.Invoke(ass);
        }
        static string testfunc(string abc, Func<string, string> str)
        {
            return str(abc);
        }

        static void Main3444f(string[] args)
        {
            // int b = fun(5, 1);

            string asd = "我是action";
            testaction(asd, (a) =>
                    Console.WriteLine(a)
                );
            string skjd = testfunc(asd, (a) =>
                 {
                     return "hello" + a;
                 }
                 );
            Console.WriteLine(skjd);
            #region Incersion Of Control学习练习
            //dynamic kk = new Dnamia();
            //var ss = kk.add(5);
            //kk.Name = "着好难";
            //Console.WriteLine(kk.Name);
            //Console.WriteLine(ss);
            //IFactory factory = new MacFactory();//FactoryTool.IFactoty_;
            //IButtonStyle button = factory.Style();
            //Console.WriteLine(button.ShowStyle());
            //todo:Inversion Of Control
            //Role ro = new Role();
            //Guai ks = new Guai("天使", 259);
            //ISword wuqi = new IronSword();
            //wuqi.AttrackExec(ks);
            //ISword wuqi1 = new WoodSword();
            //wuqi1.AttrackExec(ks);
            #endregion
            #region ///LINQ学习联系方法杨洋LINQ学习第一篇

            //List<int> Intlist = new List<int> { 3, 14, 16, 18, 45, 3, 2, 4, 5, 6, 7 };
            //var s = Intlist.JiSuan(a => a % 2 == 1);
            //foreach (var item in s)
            //{
            //    Console.WriteLine(item);
            //}

            List<Dnamia> dst = new List<Dnamia>() { 
                new Dnamia() { Name = "找好", Age = 14, Score = 28.87 ,Birthday=DateTime.Now.AddYears(-19)}, 
                new Dnamia() { Name = "总与", Age = 19, Score = 73.2,Birthday=DateTime.Now.AddYears(-18) },
                new Dnamia() { Name = "宏大", Age = 19, Score = 72 ,Birthday=DateTime.Now.AddYears(-12)},
                new Dnamia() { Name = "明显", Age = 39, Score = 16.2 ,Birthday=DateTime.Now.AddYears(-6)},
                new Dnamia() { Name = "刘洋", Age = 29, Score = 63.23,Birthday=DateTime.Now.AddYears(-7) },
                new Dnamia() { Name = "刘洋", Age = 29, Score = 63.23,Birthday=DateTime.Now.AddYears(-7) },
                new Dnamia() { Name = "张天", Age = 15, Score = 83.6 ,Birthday=DateTime.Now.AddYears(-10)},
                new Dnamia() { Name = "很甜", Age = 35, Score = 23.2,Birthday=DateTime.Now.AddYears(-9) },
                new Dnamia() { Name = "忽忽", Age = 16, Score = 0.2 ,Birthday=DateTime.Now.AddYears(-8)},
                new Dnamia() { Name = "忽忽", Age = 16, Score = 0.2 ,Birthday=DateTime.Now.AddYears(-8)}
            };

            var query = from a in dst
                        where a.Score >= 60 && a.Age >= 18 && Method1.IndexStr(a.Name, "与")
                        orderby a.Age
                        select new
                        {
                            姓名 = a.Name,
                            年龄 = a.Age,
                            学分 = a.Score
                        };
            foreach (var item in query)
            {
                Console.WriteLine(item.年龄 + "======" + item.姓名 + "======" + item.学分);
            }


            ///返回姓名和索引
            //var ResultNameIndex = dst.Select((person, index) => new { person.Name, index }).OrderBy(a => a.index);
            //var ResultNameIndex = dst.OrderBy(a => a.Age).ThenBy(a => a.Score);
            var ResultNameIndex = from a in dst
                                  orderby a.Age, a.Score descending
                                  select a;//todo:多条件可能造成排序不准确
            foreach (var item in ResultNameIndex)
            {
                Console.WriteLine(item.Age + "============" + item.Name);
            }

            ///使用自定义foreach通过ienumerable进行遍历
            dst.ForEach(p => { Console.WriteLine(p.Name + "----" + p.Score); });
            ///使用ALL表达式判断所有人员中生日是否都大于1999年
            bool ast = dst.All(p => p.Birthday > Convert.ToDateTime("1999-09-08"));//我们把All换成Any，意思就是，在studentsForEach集合中是否存在至少一个学生大于90年的
            Console.WriteLine(ast);
            string[] words = new string[] { "what", "is", "hei", "betch", "predicate", "limtd", "struggle" };

            //var wordcode = from a in words
            //               group a by a.Length into wordclub
            //               orderby wordclub.Key ascending
            //               select new
            //               {
            //                   字符数 = wordclub.Key,
            //                   单词 = wordclub
            //               };
            var wordcode = words.GroupBy(a => a.Length).OrderByDescending(a => a.Key);
            foreach (var item in wordcode)
            {
                Console.WriteLine(item.Key);
                foreach (var items in item)
                {
                    Console.WriteLine(items);
                }
            }
            ///创建一个重复值的数组
            string[] repo3 = Enumerable.Repeat("89", 5).ToArray();
            for (int i = 0; i < repo3.Length; i++)
            {
                Console.WriteLine(repo3[i]);
            }

            #endregion
            #region linq练习取数据 小练习


            //todo:用linq取出一个集合中重复的数据
            var queryrepete = from c in dst
                              group c by c.Name into g
                              where g.Count() > 1
                              select new Dnamia
                              {
                                  Age = g.Max(x => x.Age),
                                  Name = g.Key
                              };
            foreach (var item in queryrepete)
            {
                Console.WriteLine(item.Name + "==========" + item.Age);
            }
            string[] s = new string[] { "1", "2", "3", "34", "52", "52", "1", "32", "34" };
            var repeat = "";
            for (var i = 0; i < s.Length - 1; i++)
            {
                for (var j = i + 1; j < s.Length; j++)
                {
                    if (s[i] == s[j])
                    {
                        repeat += s[i] + ",";
                    }
                }
            }
            repeat = repeat.Substring(0, repeat.Length - 1);
            Console.WriteLine(repeat);
            int[] sum = new int[] { 3, 2, 5, 6, 2 };
            for (int i = 0; i < sum.Length; i++)
            {
                for (int j = i; j < sum.Length; j++)
                {
                    int temp;
                    if (sum[j] < sum[i])
                    {
                        temp = sum[i];
                        sum[i] = sum[j];
                        sum[j] = temp;
                    }

                }
                Console.WriteLine(sum[i]);
            }
            string[] wordss = new string[] { "zhao", "hao", "nan" };
            // var sd = wordss.Select(a => a.ToUpper());
            Func<string, string> str = strs => strs.ToUpper();

            var sd = wordss.Select(str);
            foreach (var item in sd)
            {
                Console.WriteLine(item);
            }
            //todo:将一个字符串分组转换某种格式
            string stra = "http%253a%252f%252fzhuhaoliang.cn%252f";//http://zhuhaoliang.cn/
            //Func<string,string[]> strfun=st=>st.Length
            string[] allWords = stra.Split('%');
            string A1 = "(%)(2)(5)(3)(a)", A2 = "(%)(2)(5)(2)(f)";

            StringBuilder sb = new StringBuilder();
            sb.Append("http");
            ////Regex rex = new Regex(A1+A2,RegexOptions.IgnoreCase|RegexOptions.Singleline);失败！！！
            ////Match M = rex.Match(stra);
            ////if (M.Success)
            ////{

            ////    for (int i = 0; i < M.Length; i++)
            ////    {
            ////       sb.Append(M.Groups[i].ToString().ToUpper());
            ////    }
            ////}
            for (int i = 1; i < allWords.Length; i++)
            {
                sb.Append("%");
                sb.Append(allWords[i].Substring(0, 4).ToUpper());
                if (allWords[i].Length > 4)
                {
                    sb.Append(allWords[i].Substring(4));
                }

            }
            Console.WriteLine(sb.ToString());
            #endregion
        }
    }
    public class Dnamia
    {
        public string Name { get; set; }
        public int Age { get; set; }
        /// <summary>
        /// 分数
        /// </summary>
        public double Score { get; set; }
        public DateTime Birthday { get; set; }
        //public int add(int a)
        //{
        //    return a * a;
        //}
    }

}
